package com.zimmur.solr.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.solr.client.solrj.SolrQuery.SortClause;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.solr.SolrUtil;
import com.zimmur.solr.common.Facet;
import com.zimmur.solr.common.SolrData;
import com.zimmur.solr.model.Designer;
import com.zimmur.solr.model.SearchableDesigner;
import com.zimmur.solr.service.ISolrDesignerService;
import com.zimmur.tools.PageUtil;
@Service("solrDesignerService")
public class SolrDesignerServiceImpl implements ISolrDesignerService {
	Logger log=LoggerFactory.getLogger(SolrDesignerServiceImpl.class);
	@Resource
	private SolrUtil<Designer> solrUtil;
	@Override
	public int add(Designer designer) {
		int flag=1;
		try {
			UpdateResponse response = solrUtil.add(designer, SolrData.CORE_DESIGNER);
			flag=response.getStatus();
			solrUtil.commit(SolrData.CORE_DESIGNER);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_DESIGNER);
			flag=-1;
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_DESIGNER);
			flag=-1;
		}
		return flag;
	}

	@Override
	public int delete(String userId) {
		int flag=1;
		try {
			UpdateResponse response = solrUtil.deleteById(userId,SolrData.CORE_DESIGNER);
			flag=response.getStatus();
			solrUtil.commit(SolrData.CORE_DESIGNER);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_DESIGNER);
			flag=-1;
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_DESIGNER);
			flag=-1;
		}
		return flag;
	}

	@Override
	public List<Designer> query(int isRecommand, int displayStatus, String tags, String key, List<SortClause> value,
			int pageNo) {
		List<Designer> designers=null;
		List<String> fqList=new ArrayList<>();
		String fq[]=null;
		String q=SearchableDesigner.ISRECOMMAND_FIELD+":"+isRecommand+" AND "+SearchableDesigner.DISPLAYSTATUS_FIELD+":"+displayStatus;
		if(tags==null || "全部".equals(tags) || "".equals(tags)){
			fqList.add(SearchableDesigner.TAGS_FIELD+":"+"*");
		}else{
			fqList.add(SearchableDesigner.TAGS_FIELD+":*"+tags+"*");
			
		}
		if(!StringUtils.isEmpty(key)){
			fqList.add(SearchableDesigner.SEARCH_KEYWORD+":*"+key+"*");
		}
		if(fqList.size()>0){
			fq=fqList.toArray(new String[fqList.size()]);
		}
		try {
			QueryResponse response= solrUtil.query(q, fq, value, pageNo, SolrData.CORE_DESIGNER);
			designers=response.getBeans(Designer.class);
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
			designers=new ArrayList<>();
		} catch (IOException e) {
			log.error(e.getMessage(),e);
			designers=new ArrayList<>();
		}
		return designers;
	}

	@Override
	public List<Designer> queryAll(int isRecommand, int displayStatus) {
		List<Designer> designers=new ArrayList<>();
		String q=SearchableDesigner.ISRECOMMAND_FIELD+":"+isRecommand+" AND "+SearchableDesigner.DISPLAYSTATUS_FIELD+":"+displayStatus;
		try {
			QueryResponse response= solrUtil.queryAll(q, SolrData.CORE_DESIGNER);
			designers=response.getBeans(Designer.class);
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return designers;
	}

	@Override
	public Map<String, Object> queryWithfacet(int isRecommand, int displayStatus, String tags, String key,
			List<SortClause> value, int pageNo,Facet facet) {
		Map<String, Object> map=new HashMap<>();
		List<Designer> designers=new ArrayList<>();
		List<String> fqList=new ArrayList<>();
		String fq[]=null;
		String q=SearchableDesigner.ISRECOMMAND_FIELD+":"+isRecommand+" AND "+SearchableDesigner.DISPLAYSTATUS_FIELD+":"+displayStatus;
		if(tags==null || "全部".equals(tags) || "".equals(tags)){
			fqList.add(SearchableDesigner.TAGS_FIELD+":"+"*");
		}
		if(!StringUtils.isEmpty(key)){
			fqList.add(SearchableDesigner.SEARCH_KEYWORD);
		}
		if(fqList.size()>0){
			fq=fqList.toArray(new String[fqList.size()]);
		}
		try {
			QueryResponse response= solrUtil.queryWithFacet(q, fq, value, pageNo, facet, SolrData.CORE_DESIGNER);
			designers=response.getBeans(Designer.class);
			map.put("designer", designers);
			map.put("facetFields", response.getFacetFields());
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return map;
	}

	@Override
	public PageUtil<Designer> queryBytabAndkey(String tags, String key, int pageNo) {
		int isRecommand=1;
		int displayStatus=1;
		PageUtil<Designer> page=new PageUtil<>();
		List<Designer> designers=null;
		List<String> fqList=new ArrayList<>();
		String fq[]=null;
		String q=SearchableDesigner.ISRECOMMAND_FIELD+":"+isRecommand+" AND "+SearchableDesigner.DISPLAYSTATUS_FIELD+":"+displayStatus;
		if(!StringUtils.isEmpty(tags))
			fqList.add(SearchableDesigner.TAGS_FIELD+":*"+tags+"*");
		if(!StringUtils.isEmpty(key))
			fqList.add(SearchableDesigner.SEARCH_KEYWORD+":"+key);
		if(fqList.size()>0){
			fq=fqList.toArray(new String[fqList.size()]);
		}
		String orderByClause=SearchableDesigner.CREATEDATE_FIELD+" desc";
		try {
			QueryResponse response= solrUtil.query(q, fq, orderByClause, pageNo, SolrData.CORE_DESIGNER);
			designers=response.getBeans(Designer.class);
			page.setItemList(designers);
			int totalResult=(int)response.getResults().getNumFound();
			page.setTotalRecord(totalResult);
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
			designers=new ArrayList<>();
		} catch (IOException e) {
			log.error(e.getMessage(),e);
			designers=new ArrayList<>();
		}
		return page;
	}

	@Override
	public int addList(List<Designer> designers) {
		int flag=1;
		try {
			UpdateResponse response = solrUtil.addList(designers, SolrData.CORE_DESIGNER);
			flag=response.getStatus();
			solrUtil.commit(SolrData.CORE_DESIGNER);
		} catch (IOException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_DESIGNER);
			flag=-1;
		} catch (SolrServerException e) {
			log.error(e.getMessage(),e);
			solrUtil.rollback(SolrData.CORE_DESIGNER);
			flag=-1;
		}
		return flag;
	}

}
